﻿using Azure.Core;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SystemMHB.API.Application.Command.WarehouseCommand;
using SystemMHB.API.DTOs;
using SystemMHB.Domian;
using SystemMHB.Domian.DTO;

namespace SystemMHB.API.Controllers
{
    /// <summary>
    /// 仓库管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WarehouseController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<WarehouseController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>
        public WarehouseController(ILogger<WarehouseController> logger, IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加仓库管理信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> WarehouseAdd(WarehouseAddCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 显示仓库管理信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PagingDTO<Warehouse>>> WarehouseShow([FromQuery] WarehouseShowCommand request)
        {
            ApiResult<PagingDTO<Warehouse>> result = new ApiResult<PagingDTO<Warehouse>>();
            try
            {
                result.Data = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        } 

        /// <summary>
        /// 反填仓库管理信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Warehouse>> WarehouseFan([FromQuery]WarehouseFanCommand request)
        {
            ApiResult<Warehouse> result = new ApiResult<Warehouse>();
            try
            {
                result.Data = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改仓库管理信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> WarehouseUpdate(WarehouseUpdateCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除仓库管理信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> WarehouseDelete(WarehouseDeleteCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 平面图
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Warehouse>>> Ichnography([FromQuery] IchnographyCommand request)
        {
            ApiResult<List<Warehouse>> result = new ApiResult<List<Warehouse>>();
            try
            {
                result.Data = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 仓库下拉列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PagingDTO<Warehouse>>> WarehouseList()
        {
            ApiResult<PagingDTO<Warehouse>> result = new ApiResult<PagingDTO<Warehouse>>();
            try
            {
                result.Data = await mediator.Send(new WarehouseListCommand());
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 根据名称擦寻仓库信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Warehouse>> WarehouseNameList([FromQuery]WarehouseNameListCommand request)
        {
            ApiResult<Warehouse> result = new ApiResult<Warehouse>();
            try
            {
                result.Data = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }







        /// <summary>
        /// 导出仓库管理
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> WarehouseXlsx([FromQuery] WarehouseShowCommand request)
        {
            try
            {
                var qoutr = await mediator.Send(request);
                IWorkbook iwkb = new XSSFWorkbook();
                ISheet isheet = iwkb.CreateSheet("仓库管理");
                IRow row = isheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("仓库ID");
                row.CreateCell(1).SetCellValue("仓库编号");
                row.CreateCell(2).SetCellValue("仓库名称");
                row.CreateCell(3).SetCellValue("面积");
                row.CreateCell(4).SetCellValue("选择仓库区域");
                row.CreateCell(5).SetCellValue("位置编号");
                row.CreateCell(6).SetCellValue("负责人");
                row.CreateCell(7).SetCellValue("状态");
                row.CreateCell(8).SetCellValue("备注");
                row.CreateCell(9).SetCellValue("删除标记");
                row.CreateCell(10).SetCellValue("添加人");
                row.CreateCell(11).SetCellValue("添加时间");
                row.CreateCell(12).SetCellValue("修改人");
                row.CreateCell(13).SetCellValue("修改时间");
                row.CreateCell(14).SetCellValue("删除人");
                row.CreateCell(15).SetCellValue("删除时间");
                int count = 1;
                foreach (var item in qoutr.plist)
                {
                    row = isheet.CreateRow(count);
                    row.CreateCell(0).SetCellValue(item.warehouseID);
                    row.CreateCell(1).SetCellValue(item.warehouseNumber);
                    row.CreateCell(2).SetCellValue(item.warehouseName);
                    row.CreateCell(3).SetCellValue(item.warehouseArea);
                    row.CreateCell(4).SetCellValue(item.selectWarehouseArea);
                    row.CreateCell(5).SetCellValue(item.positionNumber);
                    row.CreateCell(6).SetCellValue(item.principal);
                    row.CreateCell(7).SetCellValue(item.warehouseState==1?"正常":"异常");
                    row.CreateCell(8).SetCellValue(item.warehouseRemark);
                    row.CreateCell(9).SetCellValue(item.IsDel);
                    row.CreateCell(10).SetCellValue(item.AddName);
                    row.CreateCell(11).SetCellValue(item.AddTime.ToString());
                    row.CreateCell(12).SetCellValue(item.UpdateName);
                    row.CreateCell(13).SetCellValue(item.UpdateTime.ToString());
                    row.CreateCell(14).SetCellValue(item.DelectName);
                    row.CreateCell(15).SetCellValue(item.DelectTime.ToString());
                    count++;
                }
                byte[] bytes;
                using (MemoryStream ms = new MemoryStream())
                {
                    iwkb.Write(ms);
                    bytes = ms.ToArray();
                }
                string serrxlsx = new FileExtensionContentTypeProvider().Mappings[".xlsx"];

                return File(bytes, serrxlsx, "仓库管理.xlsx");
            }
            catch (Exception ex)
            {
                logger.LogError("【入库管理控制器】详情入库管理信息 出错" + ex.Message);
                throw;
            }
        }
    }
}
